/**
 * FileName: SafeAlbum.java
 * @Description: TODO(用一句话描述该文件做什么)
 * All rights Reserved, Designed By 21winmess
 * Copyright: Copyright(C) 2010-2011 
 * Company 21winmess DaLian LTD.
 * @author: Matrix xu
 * @version V2.0
 * Createdate: 2012-12-13 下午2:52:47
 *
 * Modification History:
 * Date Author Version Discription
 * -----------------------------------------------------------------------------------
 * 2012-12-13 Qute_Android2.0
 * Why & What is modified: <修改原因描述>
 */
package com.me.safebox.ui.photo;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.me.safebox.R;
import com.me.safebox.data.SafeFolder;
import com.me.safebox.data.SafeboxDao;
import com.me.safebox.service.FileConfig;
import com.me.safebox.ui.MainApp;
import com.me.safebox.ui.adapter.SafeAlbumAdatper;
import com.me.safebox.ui.lib.ActionItem;
import com.me.safebox.ui.lib.TitlePopup;
import com.me.safebox.ui.lib.TitlePopup.OnItemOnClickListener;
import com.tendcloud.tenddata.TCAgent;

/**
 * @ClassName: SafeAlbum
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: Matrix
 * @date: 2012-12-13 下午2:52:47
 * 
 */
public class SafeAlbum extends Activity implements OnClickListener, OnItemOnClickListener {

	private GridView mGridView = null;

	private TextView addPhotoBtn = null;

	private TextView addAlbumBtn = null;

	private SafeAlbumAdatper mSafeAlbumAdatper = null;

	private TitlePopup titlePopup;

	private TextView popView = null;

	private TextView titleView = null;

	private final static int QUERY_ALBUM = 1;

	private void initPopup() {
		Context context = MainApp.context;
		titlePopup = new TitlePopup(context, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		View view = LayoutInflater.from(this).inflate(R.layout.title_popup, null);
		titlePopup.addAction(new ActionItem(this, "+图片", 0));
		titlePopup.addAction(new ActionItem(this, "+相册", 0));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.layout_ablum);
		// initPopup();
		// titlePopup.setItemOnClickListener(this);
		mGridView = (GridView) findViewById(R.id.ablum_gridview);
		popView = (TextView) findViewById(R.id.pop_tv);
		popView.setOnClickListener(this);
		this.titleView = (TextView) findViewById(R.id.album_title);
		// addPhotoBtn = (TextView) findViewById(R.id.add_photo_btn);
		// addPhotoBtn.setOnClickListener(this);
		// addAlbumBtn = (TextView) findViewById(R.id.add_album_btn);
		// addAlbumBtn.setOnClickListener(this);
		mSafeAlbumAdatper = new SafeAlbumAdatper(this, MainApp.getFinalBitmap());
		mGridView.setAdapter(mSafeAlbumAdatper);
		

	}

	private void getAlbum() {
//		Thread fileScanThread = new Thread() {
//			@Override
//			public void run() {
//				List<SafeFolder> dataList = new SafeboxDao().getSafeFolder(FileConfig.FOLDER_TYPE_ALBUM);
//				Message msg = mHandler.obtainMessage();
//				msg.obj = dataList;
//				msg.what = QUERY_ALBUM;
//				mHandler.sendMessage(msg);
//			}
//		};
//		fileScanThread.start();
		List<SafeFolder> dataList = new SafeboxDao().getSafeFolder(FileConfig.FOLDER_TYPE_ALBUM);
//		Message msg = mHandler.obtainMessage();
//		msg.obj = dataList;
//		msg.what = QUERY_ALBUM;
//		mHandler.sendMessage(msg);
		int count = dataList.size();
		titleView.setText("共" + count + "个相册");
		mSafeAlbumAdatper.setData(dataList);
		mSafeAlbumAdatper.notifyDataSetChanged();

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		super.onResume();
		this.getAlbum();
		TCAgent.onResume(this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onStop()
	 */
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		mSafeAlbumAdatper.clearView();
		mSafeAlbumAdatper.clearData();
		super.onStop();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.pop_tv:

			Intent intent = new Intent(this, MediaScan.class);
			startActivity(intent);
			//finish();
			// titlePopup.show(v);
			break;
		// case R.id.add_photo_btn:
		// Intent intent = new Intent(this, MediaScan.class);
		// startActivity(intent);
		// break;
		// case R.id.add_album_btn:
		// Toast.makeText(this, "该功能正在开发中...", Toast.LENGTH_SHORT).show();
		// break;
		}
	}

	@Override
	public void onItemClick(ActionItem item, int position) {
		// TODO Auto-generated method stub
		Log.d("onItemOnClick", position + "---" + item.mTitle);
		switch (position) {
		case 0:
			Intent intent = new Intent(this, MediaScan.class);
			startActivity(intent);
			finish();
			break;
		case 1:
			Toast.makeText(this, "该功能正在开发中...", Toast.LENGTH_SHORT).show();
			break;
		}
		// Toast.makeText(this, "你点击了第" + (position + 1) + "项---" + item.mTitle,
		// Toast.LENGTH_SHORT).show();
	}

	private Handler mHandler = new Handler() {
		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case QUERY_ALBUM:
				int count = ((ArrayList<SafeFolder>) msg.obj).size();
				titleView.setText("共" + count + "个相册");
				mSafeAlbumAdatper.setData((ArrayList<SafeFolder>) msg.obj);
				mSafeAlbumAdatper.notifyDataSetChanged();
				break;
			}

		}
	};

	/**
	 * 按back键盘两次退出应用
	 */
	private static Boolean isExit = false;
	private static Boolean hasTask = false;

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (isExit == false) {
				isExit = true;
				Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
				if (!hasTask) {
					mHandler.postDelayed(new Runnable() {
						@Override
						public void run() {
							isExit = false;
							hasTask = true;
						}
					}, 2000);
				}
			} else {
				finish();
				System.exit(0);
			}
		}
		return false;
	}
}
